<?php
/**
 * 返回信息
 */
function returnData($data=[],$message="",$code = 200){
    if (empty($message)) {
        if ($code == 200) {
            $message = '操作成功';
        }elseif ($code == 400) {
            $message = '未找到相关信息';
        }elseif ($code == 500) {
            $message = '操作失败';
        }else{
            $message = '未知错误';
        }
    }
    if (empty($data)) {
        $response = json(['code'=>$code,'message'=>$message]);
    }else{
        $response = json(['code'=>$code,'message'=>$message,'data'=>$data]);
    }
    exit($response->send());
}
/**
 * 错误返回信息
 */
function returnError($message="",$code = 500){
    returnData(array(),$message,$code);
}
/**
 * 根据城市名称获取id
 */
function getShortDate($date) {
    return substr($date, 0, strpos($date, ' '));
}

// HTTP请求（支持HTTP/HTTPS，支持GET/POST）
function http_request($url, $data = null) {
    $curl = curl_init();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    //var_dump($output);
    curl_close($ch);
    return $output;
}

function sendcode($phone,$tpl_id,$tpl_value){
    $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
    //$new_code = rand(0001, 9999);
  //  $codesModel = new CodesModel();
    $smsConf = array(
        'key' => config('juhekey'), //您申请的APPKEY
        'mobile' => $phone, //接受短信的用户手机号码
        'tpl_id' =>$tpl_id, //您申请的短信模板ID，根据实际情况修改
        'tpl_value' => $tpl_value //您设置的模板变量，根据实际情况修改
    );

    $content =juhecurl($sendUrl, $smsConf, 1); //请求发送短信
    if ($content) {
        $result = json_decode($content, true);
        $error_code = $result['error_code'];
        if ($error_code == 0) {
            //状态为0，说明短信发送成功
            //echo "短信发送成功,短信ID：" . $result['result']['sid'];
            return true;
        } else {
            //状态非0，说明失败
            $msg = $result['reason'];
            //echo "短信发送失败(" . $error_code . ")：" . $msg;
            return false;
        }
    } else {
        //返回内容异常，以下可根据业务逻辑自行修改
        //echo "请求发送短信失败";
        return false;
    }
}

/**
 * 请求接口返回内容
 * @param  string $url [请求的URL地址]
 * @param  string $params [请求的参数]
 * @param  int $ipost [是否采用POST形式]
 * @return  string
 */
function juhecurl($url, $params = false, $ispost = 0) {
    $httpInfo = array();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if ($ispost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        if ($params) {
            curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }
    $response = curl_exec($ch);
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $response;
}
